<?php

/**
 * HTTP(S) 監控服務類
 * @author Eugene ESP J
 */

class http_monitor
{
	public $host;
	public $host_nodes;
	public $timeout = 2000;
	public $max_redirs = 10;

	private $ch;
	private $mh;
	private $curl_array;

	/**
	 * 构造函数
	 */
	function __construct()
	{
	}

	/**
	 * 初始化 CURL
	 */
	public function m_init()
	{
		$this->ch = curl_init();
		curl_setopt($this->ch, CURLOPT_URL, $this->host);
		curl_setopt($this->ch, CURLOPT_HEADER, FALSE);
		curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, TRUE);
		curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, TRUE);
		curl_setopt($this->ch, CURLOPT_MAXREDIRS, $this->max_redirs);
		curl_setopt($this->ch, CURLOPT_AUTOREFERER, TRUE);
		curl_setopt($this->ch, CURLOPT_FAILONERROR, TRUE);
		curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($this->ch, CURLOPT_TIMEOUT_MS, $this->timeout);
	}

	/**
	 * 多項初始化 CURL
	 */
	public function m_multi_init()
	{
		$this->mh = curl_multi_init();
		$this->curl_array = array();
		foreach($this->host_nodes as $i => $url)
		{
			$this->curl_array[$i] = curl_init($url);
			curl_setopt($this->curl_array[$i], CURLOPT_URL, $url);
			curl_setopt($this->curl_array[$i], CURLOPT_HEADER, FALSE);
			curl_setopt($this->curl_array[$i], CURLOPT_RETURNTRANSFER, TRUE);
			curl_setopt($this->curl_array[$i], CURLOPT_FOLLOWLOCATION, TRUE);
			curl_setopt($this->curl_array[$i], CURLOPT_MAXREDIRS, $this->max_redirs);
			curl_setopt($this->curl_array[$i], CURLOPT_AUTOREFERER, TRUE);
			curl_setopt($this->curl_array[$i], CURLOPT_FAILONERROR, TRUE);
			curl_setopt($this->curl_array[$i], CURLOPT_SSL_VERIFYPEER, FALSE);
			curl_setopt($this->curl_array[$i], CURLOPT_TIMEOUT_MS, $this->timeout);
			curl_multi_add_handle($this->mh, $this->curl_array[$i]);
		}
	}

	/**
	 * CURL 請求
	 */
	public function m_exec()
	{
		return (!$this->ch) ? false : curl_exec($this->ch);
	}

	/**
	 * 多項 CURL 請求
	 */
	public function m_multi_exec()
	{
		$active = NULL;
		do {
			$mrc = curl_multi_exec($this->mh, $active);
		} while ($mrc == CURLM_CALL_MULTI_PERFORM);

		while ($active && $mrc == CURLM_OK) {
			if (curl_multi_select($this->mh) != -1) {
				do {
					$mrc = curl_multi_exec($this->mh, $active);
				} while ($mrc == CURLM_CALL_MULTI_PERFORM);
			}
		}
	}

	/**
	 * CURL 獲取返回信息
	 */
	public function m_response()
	{
		return (!$this->ch) ? false : curl_getinfo($this->ch);
	}

	/**
	 * CURL 獲取多項返回信息
	 */
	public function m_multi_response()
	{
		$m_multi_res = array();
		foreach($this->host_nodes as $i => $url)
		{
			$m_multi_res[$url] = curl_getinfo($this->curl_array[$i]);
			curl_close($this->curl_array[$i]);
			curl_multi_remove_handle($this->mh, $this->curl_array[$i]);
		}
		return (!$m_multi_res) ? false : $m_multi_res;
	}


	/**
	 * 關閉 CURL 資源
	 */
	public function m_close()
	{
		curl_close($this->ch);
	}

	/**
	 * 關閉多項 CURL 資源
	 */
	public function m_multi_close()
	{
		curl_multi_close($this->mh);
	}

}

/* End of file http.class.php */
/* Location: ./cron/http.class.php */
?>